var Build = (function($){
	var eventClick = function(selector, fn){
		var ele = $(arguments[0]);
		var callBack = arguments[1];
		ele.on("click",function(){
			callBack();
		})
	};
	var oajax;
	var request = function(xhr,success, cancel){
		var url = "http://qtt.tunnel.qydev.com" + xhr.port;
		if(!cancel){
			oajax = $.ajax({
				type: xhr.type,
				url: url,
				data: xhr.data,
				dataType: "jsonp",
				jsonpCallback: "callback",
				success: function(data){
					$.hideLoading();
					success(data);
				},
				error: function(error){
					$.hideLoading();
					$.toast("请求服务器失败,请联系管理员");
				}
			});
		}else{
			oajax.abort();
		}
	};
	var saveCache = function(obj, key, value) {
		var temp = {};
		if(typeof key === "object"){
			sessionStorage[obj] = JSON.stringify(key);
			return;
		}
		if(sessionStorage[obj]){
			temp = JSON.parse(sessionStorage[obj]);
		}
		temp[key] = value;
		sessionStorage[obj] = JSON.stringify(temp);
	};
	var getCookie = function(c_name){
		if (document.cookie.length>0){
		  	c_start = document.cookie.indexOf(c_name + "=");
		  	if (c_start!=-1){ 
		    	c_start = c_start + c_name.length+1 
		    	c_end = document.cookie.indexOf(";",c_start)
			    if (c_end==-1) {
			    	c_end = document.cookie.length;
			    }
			    return console.log(document.cookie.substring(c_start,c_end))
		    } 
		  }
		return "null";
	};
	var getMsg_CallBack = function(data){
		if(data.code == "416"){
			sessionStorage.DRIVER = JSON.stringify(data.data);
			$.alert("您的订单已被接受，请等待司机联系",function(){
				window.location.href = "page_driverAllow.html"
			});
			
		};
		if(data.code == "417"){
			$.alert("开始行程,请您先支付",function(){
				window.location.href = "page_success.html";
			});
		};
		if(data.code == "419"){
			$.alert("您已到达地点",function(){
				window.location.href = "page_evaluate.html";
			});
		}
		if(data.code == "420"){
			$.alert("司机已取消订单,请重新下单",function(){
				window.location.href = "index.html";
			});
		}
	};
	var getMsg = function(cancel){
		if(!cancel){
			oajax = $.ajax({
				type: "get",
				url: "http://qtt.tunnel.qydev.com/test/getMsg",
				dataType: "jsonp",
				data: {
					show: "0",
				},
				jsonpCallback: "callback",
				success: function(data){
					getMsg_CallBack(data);
				}
			})
		}else{
			oajax.abort();
		}
	};
	return {
		eventClick: eventClick,
		request: request,
		saveCache: saveCache,
		getCookie: getCookie,
		getMsg: getMsg
	}
})(jQuery)

$(function(){
	// $.toast.prototype.defaults.duration = 1500;
	$("input").val("");
	// sidemenu
	Build.eventClick("#userInfo", Menu.showSideMenu);
	Build.eventClick(".close_btn", Menu.allClose);
	Build.eventClick("#orderlist", Menu.orderList);
	Build.eventClick("#appointment", function(){
		window.location.href = "page_appointment.html";
	});
	Build.eventClick("#bag", function(){
		window.location.href = "sideView/page_backnote.html";
	})
	Build.eventClick("#set", function(){
		window.location.href = "sideView/page_setting.html";
	})
	Build.eventClick("#download", function(){
		$.alert("请至应用宝下载全唐通APP","text");
	})
	Build.eventClick("#backview", function(){
		if (window.location.search) {
			window.history.go(-1);
		}else{
			window.location.href = "../index.html";
		}
	})
});
